home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 30 / Mac Magazin and MacEasy Magazine CD - Issue 30.iso / utilities / Mac OS X / Flurry / Flurry source / Source Folder / Gl_saver.mm < prev    next >
Text File  |  2002-01-25  |  5KB  |  190 lines

  1. #include <Cocoa/Cocoa.h>
  2. #include <OpenGL/gl.h>
  3. #include <OpenGL/glu.h>
  4. #include "gl_saver.h"
  5. #include "std.h"
  6. #include "Smoke.h"
  7. #include "Star.h"
  8. #include "Spark.h"
  9. #include "Particle.h"
  10.  
  11. // some globals
  12. long TimeFreq, TimeStart;
  13. double fTime = 0.0f;
  14. //double fTimeOffset;
  15. double TICKSPERSECOND = 12.0f;
  16. double dframe = 0.0;
  17. //int  gratuitousFlareMode = 0;
  18. float drag;
  19. double fDeltaTime;
  20. double fOldTime;
  21. float gravity = 1500000.0f;
  22.  
  23. Particle p[MAXNUMPARTICLES];
  24. int numParticles = 250;
  25. Smoke s;
  26. Star star;
  27. Spark spark[64];
  28. float starfieldColor[MAXNUMPARTICLES*4*4];
  29. float starfieldVertices[MAXNUMPARTICLES*2*4];
  30. float starfieldTextures[MAXNUMPARTICLES*2*4];
  31. int starfieldColorIndex;
  32. int starfieldVerticesIndex;
  33. int starfieldTexturesIndex;
  34.  
  35. //////////////////////////////////////////////////////////////////
  36. //////////////////////////////////////////////////////////////////
  37. // Do any initialization of the rendering context here, such as
  38. // setting background colors, setting up lighting, or performing
  39. // preliminary calculations.
  40. void GLSetupRC(void)
  41. {
  42.     int i;
  43.     
  44.     fOldTime = [NSDate timeIntervalSinceReferenceDate];
  45.     
  46.     spark[0].mystery = 1800 / 13;
  47.     spark[1].mystery = (1800 * 2) / 13;
  48.     spark[2].mystery = (1800 * 3) / 13;
  49.     spark[3].mystery = (1800 * 4) / 13;
  50.     spark[4].mystery = (1800 * 5) / 13;
  51.     spark[5].mystery = (1800 * 6) / 13;
  52.     spark[6].mystery = (1800 * 7) / 13;
  53.     spark[7].mystery = (1800 * 8) / 13;
  54.     spark[8].mystery = (1800 * 9) / 13;
  55.     spark[9].mystery = (1800 * 10) / 13;
  56.     spark[10].mystery = (1800 * 11) / 13;
  57.     spark[11].mystery = (1800 * 12) / 13;
  58.     for (i=0;i<NUMSMOKEPARTICLES;i++)
  59.     {
  60.         s.p[i].dead = TRUE;
  61.     }
  62.  
  63. //    HDC hDC = GetDC(hWnd);
  64.     
  65.     // setup the defaults for OpenGL
  66.     glDisable(GL_DEPTH_TEST);
  67. //    glAlphaFunc(GL_GREATER,0.03125f);
  68.     glAlphaFunc(GL_GREATER,0.0f);
  69.     glEnable(GL_ALPHA_TEST);
  70.     glShadeModel(GL_FLAT);
  71. //    glEnable(GL_DITHER);
  72.     glDisable(GL_LIGHTING);
  73.     glDisable(GL_CULL_FACE);
  74.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  75.     glEnable(GL_BLEND);
  76.     glViewport(0,0,(int) sys_glWidth,(int) sys_glHeight);
  77.     glMatrixMode(GL_PROJECTION);
  78.     glLoadIdentity();
  79.     gluOrtho2D(-0.5,sys_glWidth+0.5,-0.5,sys_glHeight+0.5);
  80.     glMatrixMode(GL_MODELVIEW);
  81.     glLoadIdentity();
  82.     glClear(GL_COLOR_BUFFER_BIT);
  83. //    SwapBuffers(hDC);
  84. //    glClear(GL_COLOR_BUFFER_BIT);
  85.     //    ReleaseDC(hWnd, hDC );
  86.     sys_Time = 0;
  87.         
  88.     for (i=0;i<12;i++)
  89.     {
  90.         spark[i].Update();
  91.     }
  92.     glEnableClientState(GL_COLOR_ARRAY);    
  93.     glEnableClientState(GL_VERTEX_ARRAY);    
  94.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  95. }
  96.  
  97. //////////////////////////////////////////////////////////////////
  98. //////////////////////////////////////////////////////////////////
  99. // Render the OpenGL Scene here. Called by the WM_PAINT message
  100. // handler.
  101. void GLRenderScene(void)
  102. {
  103.     static NSDate *oldDate = NULL;
  104.     int i;
  105.     dframe++;
  106.     
  107.     //GetMouse(&mouse);
  108.     MouseX = 0;//mouse.h;
  109.     MouseY = 0;//mouse.v;
  110.     
  111.     MouseDown = 0;//Button();
  112.     
  113.     //
  114.     
  115.     if (!oldDate)
  116.         oldDate = [[NSDate date] retain];
  117.     
  118.     fOldTime = fTime;
  119.     fTime = -[oldDate timeIntervalSinceNow];
  120.     fDeltaTime = fTime - fOldTime;
  121.     
  122.     //NSLog(@"fTime = %f fDeltaTime = %f", fTime, fDeltaTime);
  123.     
  124.     drag = (float) pow(0.9965,fDeltaTime*85.0);
  125.     
  126.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  127.     glColor4f(0.0,0.0,0.0,5.0 * fDeltaTime);
  128.     glRectd(-1,-1,sys_glWidth+1,sys_glHeight+1);
  129.     
  130.     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  131.     
  132.     for (int i=0;i<numParticles;i++)
  133.     {
  134.         p[i].Update();
  135.     }
  136.     star.Update();
  137.     for (i=0;i<numStreams;i++)
  138.     {
  139.         spark[i].Update();
  140.     }
  141.     s.Update();
  142.     //if ((fTime - fTimeOffset) > 0.05f)
  143.     {
  144.         glEnable(GL_TEXTURE_2D);
  145.         s.Draw();
  146.         /*glBegin(GL_QUADS);
  147.         starfieldColorIndex = 0;
  148.         starfieldVerticesIndex = 0;
  149.         starfieldTexturesIndex = 0;
  150.         for (i=0;i<numParticles;i++)
  151.         {
  152.             p[i].Draw();
  153.         }
  154.         glColorPointer(4,GL_FLOAT,0,starfieldColor);
  155.         glVertexPointer(2,GL_FLOAT,0,starfieldVertices);
  156.         glTexCoordPointer(2,GL_FLOAT,0,starfieldTextures);
  157.          glDrawArrays(GL_QUADS,0,numParticles*4);
  158.          glEnd();
  159.         star.Draw();
  160.         for (int i=0;i<13;i++)
  161.         {
  162.             spark[i].Draw();
  163.         }*/
  164.         glDisable(GL_TEXTURE_2D);
  165.     }
  166. }
  167.  
  168.  
  169. //////////////////////////////////////////////////////////////////
  170. //////////////////////////////////////////////////////////////////
  171. // Called by Windows when it receives the WM_SIZE message.
  172. // Put any code needed here to recalc the viewing volume and
  173. // viewport info. For a screen saver, this only occurs once.
  174. void GLResize(GLsizei w, GLsizei h)
  175. {
  176.     if( h == 0 )
  177.         h = 1;
  178.     // Set viewport to our dimensions.
  179.     glViewport( 0, 0, w, h);
  180.     sys_glWidth = (float) w;
  181.     sys_glHeight = (float) h;
  182.     glMatrixMode(GL_PROJECTION);
  183.     glLoadIdentity();
  184.     gluOrtho2D(-0.5,sys_glWidth+0.5,-0.5,sys_glHeight+0.5);
  185.     glMatrixMode(GL_MODELVIEW);
  186.     //glScissor(0,0,(int) sys_glWidth,(int) sys_glHeight);
  187.     //glEnable(GL_SCISSOR_TEST);
  188. }
  189.  
  190.